
#include <BGridLayout>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BLabel>
#include <BLineEdit>
#include <BButton>
#include <BFont>
#include <BFolder>
#include <BEntry>
#include <BSplitter>
#include <BStyle>
#include <BTableItem>
#include <BFileDialog>

#include "MainWindow.h"

MainWindow::MainWindow()
{
	_colorDialog = new BColorDialog(this);

	_board = new BWidget();
	{
		_fontNameEdit = new BLineEdit();
		_fontNameEdit->setText("this is Line Edit");
		_fontComboBox = new BComboBox();
		_fontComboBox->addItem("item0");
		_fontComboBox->addItem("item1");
		_fontComboBox->addItem("item2");
		_fontComboBox->addItem("item3");
		_treeWidget = new BTreeWidget();
		_treeWidget->setSizeStretch(100);
		BTreeItem* topItem = _treeWidget->addTopItem("treeWidget");
		topItem->addChild("item0");
		topItem->addChild("item1");
		topItem->addChild("item2");

		BVBoxLayout* boardLayout = new BVBoxLayout(_board);
		boardLayout->addWidget(_fontNameEdit);
		boardLayout->addWidget(_fontComboBox);
		boardLayout->addWidget(_treeWidget);
	}

	_editer = new BWidget();
	{
		_applyButton = new BButton("apply");
		_saveButton = new BButton("save");
		_loadButton = new BButton("load");

		_textEdit = new BTextEdit();
		_textEdit->setHeightStretch(100);
		_textEdit->setBorder(3);

		BHBoxLayout* hLayout = new BHBoxLayout();
		hLayout->addWidget(_applyButton);
		hLayout->addWidget(_saveButton);
		hLayout->addWidget(_loadButton);

		BVBoxLayout* editLayout = new BVBoxLayout(_editer);
		editLayout->addLayout(hLayout);
		editLayout->addWidget(_textEdit);
	}

	BSplitter* splitter = new BSplitter();
	splitter->addWidget(_board);
	splitter->addWidget(_editer);

	BVBoxLayout* layout = new BVBoxLayout(this);
	layout->addWidget(splitter);

	connect(_fontComboBox, Signal_CurrentChanged, &MainWindow::procCurrent);
	connect(_applyButton, Signal_Clicked, &MainWindow::procApply);
	connect(_saveButton, Signal_Clicked, &MainWindow::procSave);
	connect(_loadButton, Signal_Clicked, &MainWindow::procLoad);
	connect(_treeWidget, Signal_ItemClicked, &MainWindow::procItemClicked);

	_textEdit->setText("input sheet over here, then click apply!");
}
MainWindow::~MainWindow()
{

}

void MainWindow::applySheet()
{
	const BString& text = _textEdit->text();
	this->setStyleSheet(text);
}

void MainWindow::procCurrent(BObject* object, const BValue& value)
{
	close();
}
void MainWindow::procApply(BObject* object, const BValue& value)
{
	applySheet();
}
void MainWindow::procSave()
{
	BHolder<BFileDialog> dialog = new BFileDialog(this);
	if (dialog->execute())
	{
		const BString& fileName = dialog->fileName();
	}
}
void MainWindow::procLoad()
{

}

void MainWindow::procItemClicked(BObject* object, const BValue& value)
{
	BTableItem* item = value;
	if (item && item->column() == 1)
	{
		_colorDialog->setColor(item->color());
		if (_colorDialog->execute())
		{
			const BColor& color = _colorDialog->color();
			item->setColor(color);
		}
	}
}
